
package com.rolemodule.dao;
import com.rolemodule.bean.*;
import java.sql.*;
import java.util.*;
import util.*;
public class TbRoleDAO {
    private Connection conn = null;
    private PreparedStatement pt = null;
    private ResultSet rs = null;
    private boolean flag = false;
    private String sql = "";
    private TbRole tbRole=null;
    private List<TbRole> tbRoleList=null;

    public boolean add(TbRole inTbRole){
        try{
            
        conn=ConnectDB.getSqlServerConnection();
        sql="insert into Tb_Role(RoleName,UserId,RoleMemo,CreateTime) values(?,?,?,?)";
        pt = conn.prepareStatement(sql);
        
        pt.setString(1,inTbRole.getRoleName());
        pt.setInt(2,inTbRole.getUserId());
        pt.setString(3,inTbRole.getRoleMemo());
        pt.setString(4,inTbRole.getCreateTime());
        if (pt.executeUpdate() > 0) flag=true;
        }catch(Exception e){
            e.printStackTrace();
        }finally {
    ConnectDB.closeSqlConnection();
            
        }
        return flag;
    }
    public boolean del(int inRoleId){
        return UtilDAO.del("Tb_Role", "RoleId", inRoleId);
    }
    public boolean edit(TbRole inTbRole){
        try{
            
        conn=ConnectDB.getSqlServerConnection();
        sql="update Tb_Role set RoleId=?,RoleName=?,UserId=?,RoleMemo=?,CreateTime=? where RoleId=?";
        pt = conn.prepareStatement(sql);
        
        pt.setInt(1,inTbRole.getRoleId());
        pt.setString(2,inTbRole.getRoleName());
        pt.setInt(3,inTbRole.getUserId());
        pt.setString(4,inTbRole.getRoleMemo());
        pt.setString(5,inTbRole.getCreateTime());
        pt.setInt(6,inTbRole.getRoleId());
        if (pt.executeUpdate() > 0) flag=true;
        }catch(Exception e){
            e.printStackTrace();
        }finally {
    ConnectDB.closeSqlConnection();
            
        }
        return flag;
    }
    public TbRole getTbRoleByRoleId(int inRoleId) {
        tbRole=null;
        try{
            conn = ConnectDB.getSqlServerConnection();
            sql = "select * from Tb_Role where RoleId=? ";
            pt = conn.prepareStatement(sql);
            pt.setInt(1,inRoleId);
            rs = pt.executeQuery();
            while (rs.next()) {
                if (tbRole == null)tbRole = getTbRole(rs);
            }
        }catch(Exception e){
            e.printStackTrace();
        }finally {
    ConnectDB.closeSqlConnection();
            
        }
        return tbRole;
    }
    public List<TbRole> getTbRoleAll() {
        tbRoleList=new ArrayList<TbRole>();
        try{
            conn = ConnectDB.getSqlServerConnection();
            sql = "select * from Tb_Role";
            pt = conn.prepareStatement(sql);
            rs = pt.executeQuery();
            while (rs.next()) {
                tbRoleList.add(getTbRole(rs));
            }
        }catch(Exception e){
            e.printStackTrace();
        }finally {
    ConnectDB.closeSqlConnection();
            
        }
        return tbRoleList;
    }
    public List<TbRole> getTbRoleAll(VskPage vp) {
        tbRoleList=new ArrayList<TbRole>();
        try{
            conn = ConnectDB.getSqlServerConnection();
            sql = "select * from Tb_Role";
            vp.setAllCountSql(sql);
            vp.getAllRecordCount(conn, vp.getAllCountSql());
            pt = conn.prepareStatement(vp.processPageSql( "Tb_Role", "RoleId"));
            rs = pt.executeQuery();
            while (rs.next()) {
                tbRoleList.add(getTbRole(rs));
            }
        }catch(Exception e){
            e.printStackTrace();
        }finally {
    ConnectDB.closeSqlConnection();
            
        }
        return tbRoleList;
    }
    public TbRole getTbRole(ResultSet rs) throws Exception{
        TbRole tbRole = new TbRole();
        tbRole.setRoleId(rs.getInt("RoleId"));
        tbRole.setRoleName(rs.getString("RoleName"));
        tbRole.setUserId(rs.getInt("UserId"));
        tbRole.setRoleMemo(rs.getString("RoleMemo"));
        tbRole.setCreateTime(rs.getString("CreateTime"));
        return tbRole;
    }
}
